﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using LDTB.Service;

namespace LDTB
{
    public partial class CatOptFrm : Form
    {
        LDTBDataDataContext ds = DataService.DB;

        public CatOptFrm()
        {
            InitializeComponent();
        }

        private void CatOptFrm_Load(object sender, EventArgs e)
        {

            FillToFrm();
        }

        public void FillToFrm() {


            var cat = from c in ds.OptionCates
                      select new {ID=c.ID,DegreeName=c.DegreeName };

            dataGridView1.DataSource = cat;

        }

        private void button1_Click(object sender, EventArgs e)
        {

            CatOptAddFrm caf = new CatOptAddFrm();
            caf.ShowDialog();
            FillToFrm();

        }

        private void button2_Click(object sender, EventArgs e)
        {

            int catopid = int.Parse(dataGridView1.CurrentRow.Cells[0].Value.ToString());

            CatOptEditFrm ce = new CatOptEditFrm(catopid);
            ce.ShowDialog();
            FillToFrm();



        }

        private void button4_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button3_Click(object sender, EventArgs e)
        {

            int catopid = (int)dataGridView1.CurrentRow.Cells[0].Value;

            var people = from p in ds.Peoples
                         join c in ds.OptionCates
                         on p.CatID equals c.ID
                         where c.ID == catopid
                         select p;

            var cats = (from cat in ds.OptionCates
                        where cat.ID == catopid
                        select cat).FirstOrDefault();

            DialogResult dr = MessageBox.Show("Có " + people.Count() + " người thuộc mục này có chắc  nếu xóa sẽ mất dữ liệu người này ", "Xóa mục " + cats.DegreeName, MessageBoxButtons.YesNo);
            if (dr == DialogResult.Yes)
            {
                ds.Peoples.DeleteAllOnSubmit(people);
                ds.OptionCates.DeleteOnSubmit(cats);
                ds.SubmitChanges();
                FillToFrm();
                MessageBox.Show("Đã xóa thành công ");
            }

        }



    }
}
